home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS04.ADF / C / SetSerial.c < prev    next >
C/C++ Source or Header  |  1985-10-30  |  6KB  |  181 lines

  1.  
  2. /*set serial program */
  3.  
  4. /* Amicus Changes: */
  5. /* Upgraded to AmigaDOS v1.0 by John Foust, 26-Oct-85 */
  6.  
  7. /********** setserial.c *********************/
  8. /* SetSerial allows the CLI user to dynamically change any particular
  9.  * serial port parameter.
  10.  *
  11.  * July 6, 1985  Version 1.0  Keith Stobie  Initial version 
  12.  * July 7, 1985  Version 2.0  Keith Stobie  Converted to Lattice
  13.  * July 8, 1985  Version 3.0  Keith Stobie  Both term words settable.
  14.  *
  15.  */
  16.  
  17. char *prog_name;            /* Name of program being run */
  18. char *prog_version = "3.1"; /* Version of the program */
  19.                               /* was 3.0  -jjf */
  20.  
  21. #include        <exec/types.h>
  22. #include        <exec/nodes.h>
  23. #include        <exec/lists.h>
  24. #include        <exec/memory.h>
  25. #include        <exec/ports.h>
  26. #include        <exec/libraries.h>
  27. #include        <exec/devices.h>
  28. #include        <exec/tasks.h>
  29. #include        <exec/io.h>
  30. #include        <devices/serial.h>
  31. #undef NULL
  32. #include        <lattice/stdio.h>
  33.  
  34.  
  35. extern struct MsgPort *CreatePort();
  36.  
  37. int set_serial( index, value )
  38.    int   index;         /* which parameter to set */
  39.    ULONG value;      /* The new value for paramter */
  40. {
  41.     struct IOExtSer IORser;      /* Serial port IO request block */
  42.     int     error;
  43.  
  44.     if ((index < 0) || (index > 11)) {
  45.        printf( "Index value %ld is not in range 0-10!\n", index );    
  46.        return 20;
  47.     }
  48.  
  49.     if ((error = OpenDevice (SERIALNAME, 0, &IORser, 0)) != 0) {
  50.         printf( "Unable to open Serial Device, error=%ld\n", error );
  51.         return 20;
  52.     }
  53.  
  54.     /* SET UP the read message port in the I/O request */
  55.     if ((IORser.IOSer.io_Message.mn_ReplyPort = CreatePort( "SetSerial", 0 ))
  56.          == NULL) {
  57.         printf( "Unable to create port for IO message\n" );
  58.         CloseDevice( &IORser );
  59.         return 20;
  60.       }
  61.  
  62.     switch( index ) {
  63.       case 0:    print_request( &IORser );         break;
  64.       case 1:    IORser.io_CtlChar       = value;  break;
  65.       case 2:    IORser.io_RBufLen       = value;  break;
  66.       case 3:    IORser.io_WBufLen       = value;  break;
  67.       case 4:    IORser.io_Baud          = value;  break;
  68.       case 5:    IORser.io_BrkTime       = value;  break;
  69.       case 6:    IORser.io_TermArray.TermArray0 = value; break;
  70.       case 7:    IORser.io_TermArray.TermArray1 = value; break;
  71.       case 8:    IORser.io_ReadLen       = value;  break;
  72.       case 9:    IORser.io_WriteLen      = value;  break;
  73.       case 10:   IORser.io_StopBits      = value;  break;
  74.       case 11:   IORser.io_SerFlags      = value;  break;
  75.       default:   printf( "Internal Logic error! case value %ld\n", index );
  76.     } /* switch */
  77.  
  78.     IORser.IOSer.io_Flags   = 0;
  79.     IORser.IOSer.io_Command = SDCMD_SETPARAMS;
  80.  
  81.     error = DoIO( &IORser );
  82.  
  83.     DeletePort( IORser.IOSer.io_Message.mn_ReplyPort );
  84.  
  85.     CloseDevice( &IORser );
  86.  
  87.     if (error) {
  88.        printf( "Error %ld doing IO to set params!\n", error );
  89.        return 10;
  90.     }
  91.       
  92.     if (IORser.IOSer.io_Error) {
  93.        printf( "Error %ld from serial device doing set params!\n"
  94.              , IORser.IOSer.io_Error );
  95.        return 10;
  96.     }
  97.  
  98.     return 0;
  99. }  /* set_serial() */
  100.  
  101.  
  102.  
  103.  
  104. print_request( IORser )
  105.     struct IOExtSer *IORser;     /* Serial port IO request block */
  106. {
  107. #define PRINT( field )           printf( "      %s = %8lx %8ld\n" \
  108.                                  , "field", (ULONG) IORser->field \
  109.                                  , (ULONG) IORser->field )
  110. #define IPRINT( index, field ) printf( " %2ld   %s = %8lx %8ld\n" \
  111.                                     , index, "field", (ULONG) IORser->field \
  112.                                                     , (ULONG) IORser->field )
  113.  
  114.       printf( "index field name       hexadec  decimal\n" ); 
  115.       IPRINT( 1, io_CtlChar   ); 
  116.       IPRINT( 2, io_RBufLen   );
  117.       IPRINT( 3, io_WBufLen   ); 
  118.       IPRINT( 4, io_Baud      );
  119.       IPRINT( 5, io_BrkTime   ); 
  120.       IPRINT( 6, io_TermArray.TermArray0   );
  121.       IPRINT( 7, io_TermArray.TermArray1   );
  122.       IPRINT( 8, io_ReadLen   );
  123.       IPRINT( 9, io_WriteLen  );
  124.       IPRINT(10, io_StopBits  );
  125.       IPRINT(11, io_SerFlags  );
  126.       printf( "\n" );   /* Not associated with an index */
  127.           PRINT( io_Status    );
  128. }  /* print_request() */
  129.  
  130.  
  131. print_usage() {
  132.    printf("%s: version %s\n", prog_name, prog_version );
  133.    printf("usage: %s <index> <value>\n", prog_name );
  134.    printf("  <index> is a decimal number indicating which parameter.\n" );
  135.    printf("          0 indicates print current values (and indexes) \n");
  136.    printf("            without changing them.\n" );
  137.    printf("  <value> number to set the indexed parameter to.\n");
  138.    printf("          value should be in decimal unless it starts with X\n" );
  139.    printf("          or x in which case the number should be hexadecimal\n");
  140.    exit( 5 );
  141. }
  142.  
  143.  
  144.  
  145.  
  146.  
  147. main( argc, argv ) 
  148.    int argc;
  149.    char *argv[];
  150. {
  151.    int   index;
  152.    ULONG value;
  153.  
  154.    if (argc <=0 ) { prog_name = "SetSerial"; }
  155.    else { prog_name = argv[0];}
  156.  
  157.    if (argc == 1) { print_usage(); }
  158.  
  159.    if (argc < 2 ) { printf( "Too few parameters\n" ); print_usage();}
  160.    if (argc > 3 ) { printf( "Too many parameters\n" ); print_usage();}
  161.  
  162.    sscanf( *++argv, "%d", &index );
  163.  
  164.    if ((index != 0) && (argc < 3)) {
  165.       printf( "Too few parameters\n" ); print_usage();
  166.    }
  167.  
  168.    if (argc == 3) { 
  169.       ++argv;
  170.       if ((*argv[0] == 'x') || (*argv[0] == 'X')) {
  171.          sscanf( *argv+1, "%x", &value ); /* Skip x or X */
  172.       } else {
  173.          sscanf( *argv, "%d", &value );
  174.       }
  175.    }
  176.  
  177.    exit( set_serial( index, value ) ); 
  178.    
  179. }  /* main() */
  180.  
  181.